<?php

namespace App\Models\Users;

use App\Models\BasicModel;
use App\Models\User;
use Dcat\Admin\Traits\HasDateTimeFormatter;


class MoneyLog extends BasicModel
{
	use HasDateTimeFormatter;


    protected $table = 'users_money_log';

    //type 1充值2消费3预约收入4咨询收入 5占卜收入 6烧香 7提现失败退回
    public function add($uid,$amount,$desc,$type=2,$oid=0)
    {
        $user=User::query()->find($uid);
        $bf_money=$user->money;
        if($type==3 || $type==7){
            $user->earning=bcadd($user->money,$amount,2);
        }
        $af_money=bcadd($user->money,$amount,2);
        $user->money=$af_money;
        $user->save();
        return self::create([
            'user_id'=>$uid,
            'money' =>$amount,
            'before' =>$bf_money,
            'after' =>$af_money,
            'type' =>$type,
            'desc' =>$desc,
            'from' =>$oid
        ]);

    }
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}
